/**
 *	bbx 
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');

	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var exambookingService = require('ExamBooking_Service');
	var baseService = require("Base_Service");
	var enumHelper = require('Common_Enum');
	var systemService = require('System_Service');
	var accountService = require('Account_Service');
	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	var userInfo;
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			console.log("init");
			userInfo = baseService.getUser();
			mui.init({
				statusBarBackground: '#f7f7f7'
			});
			WindowTools.preloadTemplate();
			init();
		});
	}

	function init() {
		systemService.getCitys(function(data) {
			//			debugger;
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var li;
				li = document.createElement('li');
				if(userInfo.CityID==item.id){
					li.className = 'mui-table-view-cell mui-selected';
				}else{
					li.className = 'mui-table-view-cell';
				}
				
				li.setAttribute("value", item.id);
				li.setAttribute("namevalue", item.name);
				li.innerHTML = '<a class="mui-navigate-right">'+item.name+'</a>';
				fragment.appendChild(li);
			}
			var dataContainer = document.getElementById('citys');
			dataContainer.appendChild(fragment);
			initListeners();
		},function(){
//			document.getElementById('refresh').style.display = '';
			
		});
	}
	
	function initListeners() {
		mui(".mui-table-view").on("tap", ".mui-table-view-cell", function(){
			var cityId = this.getAttribute('value');
			var cityName = this.getAttribute('namevalue');
			console.log(cityId);
			if(typeof(plus) != 'undefined') {
				plus.nativeUI.showWaiting('加载中...');
			}
			accountService.setCity({
				CityID:cityId
			},function(retval){
				debugger;
				userInfo.CityID = cityId;
				userInfo.CityName = cityName;
				baseService.saveUser(userInfo);
				if(plus){
					var opener = plus.webview.currentWebview().opener();
					mui.fire(opener, "mainrefresh");
					plus.webview.currentWebview().close();
				}
			})
		});
	}

	function initLogin() {
		//检查 "登录状态/锁屏状态" 结束
		var loginButton = document.getElementById('login'); //登录按钮
		var accountBox = document.getElementById('account'); //用户名
		var passwordBox = document.getElementById('password'); //密码
		var autoLoginButton = document.getElementById("autoLogin"); //自动登录
		var regButton = document.getElementById('reg'); //注册
		var forgetButton = document.getElementById('forgetPassword'); //忘记密码
		loginButton.addEventListener('tap', function(event) {
			var loginInfo = {
				UserAccount: accountBox.value,
				UserPassword: passwordBox.value,
				UserType: 3
			};
			console.log("login");
			accountService.login(loginInfo, function(res) {
				//				debugger;
				var style = {};
				WindowTools.openWinWithTemplate(null, "../index.html", null, style);
			});

		});

		regButton.addEventListener('tap', function(event) {
			var style = {};
			WindowTools.openWinWithTemplate(null, "./reg.html", null, style);
		}, false);
		forgetButton.addEventListener('tap', function(event) {
			var style = {};
			WindowTools.openWinWithTemplate(null, "./forget_password.html", null, style);
		}, false);
	}
});